package org.radsimplified.genapp.metadata;

import org.radsimplified.genapp.Helper;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Attribute {

    protected BizEntity entity;
    protected String name;
    protected String columnName;
    protected Boolean computed = false;
    protected String label;
    protected String listLabel;
    protected String dataType;
    protected String defaultValue;
    protected Boolean nullable = true;
    protected Boolean unigue = false;
    protected Boolean visible = true;
    protected Boolean sensitive = false;
    protected Boolean visibleInList = true;
    protected Boolean searchable = true;
    protected Boolean blankNull = true;
    protected BigInteger min;
    protected BigInteger max;
    protected Byte cols;
    protected Byte rows;
    protected String pattern;
    protected String helpText;
    protected String yearRange;
    protected BizEntity relatedEntity;
    protected String mappedBy;
    protected String orderBy = "slNo";
    protected String fetchType;
    protected String cascadeType;
    protected List<SelOption> selOptions = new ArrayList<SelOption>();
    protected Boolean editable = true;

	public void setEntity(BizEntity entity) {
		this.entity = entity;
    }

	public BizEntity getEntity() {
		return entity;
	}

    public void setName(String name) {
		this.name = name != null && name.equals("") ? null : name;
    }

	public String getName() {
		return name;
	}

    public void setColumnName(String columnName) {
		this.columnName = columnName != null && columnName.equals("") ? null : columnName;
    }

	public String getColumnName() {
		return columnName;
	}

    public void setComputed(Boolean computed) {
		this.computed = computed;
    }

	public Boolean getComputed() {
		return computed;
	}

    public void setLabel(String label) {
		this.label = label != null && label.equals("") ? null : label;
    }

	public String getLabel() {
		return label == null ? "" : label;
	}

    public void setListLabel(String listLabel) {
		this.listLabel = listLabel != null && listLabel.equals("") ? null : listLabel;
    }

	public String getListLabel() {
		return listLabel;
	}

    public void setDataType(String dataType) {
		this.dataType = dataType != null && dataType.equals("") ? null : dataType;
    }

	public String getDataType() {
		return dataType;
	}

    public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue != null && defaultValue.equals("") ? null : defaultValue;
    }

	public String getDefaultValue() {
		return defaultValue;
	}

    public void setNullable(Boolean nullable) {
		this.nullable = nullable;
    }

	public Boolean getNullable() {
		return nullable;
	}

    public void setUnigue(Boolean unigue) {
		this.unigue = unigue;
    }

	public Boolean getUnigue() {
		return unigue;
	}

    public void setVisible(Boolean visible) {
		this.visible = visible;
    }

	public Boolean getVisible() {
		return visible;
	}

    public void setSensitive(Boolean sensitive) {
		this.sensitive = sensitive;
    }

	public Boolean getSensitive() {
		return sensitive;
	}

    public void setVisibleInList(Boolean visibleInList) {
		this.visibleInList = visibleInList;
    }

	public Boolean getVisibleInList() {
		return visibleInList;
	}

    public void setSearchable(Boolean searchable) {
		this.searchable = searchable;
    }

	public Boolean getSearchable() {
		return searchable;
	}

    public void setBlankNull(Boolean blankNull) {
		this.blankNull = blankNull;
    }

	public Boolean getBlankNull() {
		return blankNull;
	}

    public void setMin(BigInteger min) {
		this.min = min;
    }

	public BigInteger getMin() {
		return min;
	}

    public void setMax(BigInteger max) {
		this.max = max;
    }

	public BigInteger getMax() {
		return max;
	}

    public void setCols(Byte cols) {
		this.cols = cols;
    }

	public Byte getCols() {
		return cols;
	}

    public void setRows(Byte rows) {
		this.rows = rows;
    }

	public Byte getRows() {
		return rows;
	}

    public void setPattern(String pattern) {
		this.pattern = pattern != null && pattern.equals("") ? null : pattern;
    }

	public String getPattern() {
		return pattern;
	}

    public void setHelpText(String helpText) {
		this.helpText = helpText != null && helpText.equals("") ? null : helpText;
    }

	public String getHelpText() {
		return helpText;
	}

    public void setYearRange(String yearRange) {
		this.yearRange = yearRange != null && yearRange.equals("") ? null : yearRange;
    }

	public String getYearRange() {
		return yearRange;
	}

    public void setRelatedEntity(BizEntity relatedEntity) {
		this.relatedEntity = relatedEntity;
    }

	public BizEntity getRelatedEntity() {
		return relatedEntity;
	}

    public void setMappedBy(String mappedBy) {
		this.mappedBy = mappedBy != null && mappedBy.equals("") ? null : mappedBy;
    }

	public String getMappedBy() {
		return mappedBy;
	}

    public void setOrderBy(String orderBy) {
		this.orderBy = orderBy != null && orderBy.equals("") ? null : orderBy;
    }

	public String getOrderBy() {
		return orderBy;
	}

    public void setFetchType(String fetchType) {
		this.fetchType = fetchType != null && fetchType.equals("") ? null : fetchType;
    }

	public String getFetchType() {
		return fetchType;
	}

    public void setCascadeType(String cascadeType) {
		this.cascadeType = cascadeType != null && cascadeType.equals("") ? null : cascadeType;
    }

	public String getCascadeType() {
		return cascadeType;
	}

    public void addSelOption(SelOption obj) {
    	selOptions.add(obj);
    	obj.setAttribute((Attribute)this);
    }
    
    public void removeSelOptions(Iterator<SelOption> iter, SelOption obj) {
    	obj.setAttribute(null);
    	iter.remove();
    }

	public List<SelOption> getSelOptions() {
		return selOptions;
	}

    public void setEditable(Boolean editable) {
		this.editable = editable;
    }

	public Boolean getEditable() {
		return editable;
	}
	
	public String getTitleName() {
		return Helper.toTitleCase(name);
	}
	
	public String getGetter() {
		return "get" + getTitleName() + "()";
	}
	
	public boolean isManyToOne() {
		return dataType.equals("ManyToOne") || dataType.equals("ManyToOneSelect");
	}
	
	public String getDefStyle() {
		String type = dataType + "~";
		if ("Byte~Short~Integer~Long~BigInteger~Float~Double~BigDecimal~".contains(type))
			return "Number";
		if ("Boolean~".contains(type))
			return "Boolean";
		if ("String~Password~".contains(type))
			return "String";
		if ("Date~".contains(type))
			return "Date";
		if ("Time~".contains(type))
			return "Time";
		if ("DateTime~".contains(type))
			return "DateTime";
		if ("Text~".contains(type))
			return "Text";
		if ("Image~File~".contains(type))
			return "File";
		if ("ManyToOne~ManyToOneSelect~".contains(type))
			return "ManyToOne";
		if ("OneToMany~".contains(type))
			return dataType;

		throw new RuntimeException("Unknown DataType: " + dataType);
	}
	
	public String getViewStyle() {
		String type = dataType + "~";
		if ("Byte~Short~Integer~Long~BigInteger~Float~Double~BigDecimal~".contains(type))
			return "Number";
		if ("Date~".contains(type))
			return "Date";
		if ("DateTime~".contains(type))
			return "DateTime";
		if ("String~Boolean~Text~Password~Date~DateTime~Time~".contains(type))
			return "Simple";
		if ("String~Boolean~Text~Password~Date~DateTime~Time~".contains(type))
			return "Simple";
		if ("Image~".contains(type))
			return "Image";
		if ("File~".contains(type))
			return "File";
		if ("ManyToOne~ManyToOneSelect~".contains(type))
			return "ManyToOne";
		if ("OneToMany~".contains(type))
			return dataType;
		
		throw new RuntimeException("Unknown DataType: " + dataType);
	}
	
	public String getEditStyle() {
		String type = dataType + "~";
			
		if ("Byte~Short~Integer~Long~BigInteger~Float~Double~BigDecimal~".contains(type))
			return "Number";
		if ("Boolean~".contains(type))
			return "Boolean";
		if ("String~".contains(type))
			return "String";
		if ("Password~".contains(type))
			return "Password";
		if ("Date~".contains(type))
			return "Date";
		if ("DateTime~".contains(type))
			return "DateTime";
		if ("Time~".contains(type))
			return "Time";
		if ("Text~".contains(type))
			return "Text";
		if ("Image~File~".contains(type))
			return "File";
		if ("ManyToOne~ManyToOneSelect~".contains(type))
			return "ManyToOne";
		if ("OneToMany~".contains(type))
			return dataType;
		throw new RuntimeException("Unknown DataType: " + dataType);
	}

	public String getCodeDataType() {
		
		if ("Password~Text~Select".contains(dataType))
			return "String";
		if (dataType.equals("DateTime"))
			return "Date";
		if ("Image~File".contains(dataType))
			return "byte[]";
		if (relatedEntity != null)
			return relatedEntity.getName();
		return dataType;
		
	}

}
